#include "Board.h"
#include "stdio.h"
#include "usart.h"
#include "delay.h"
#include "Aip33624.h"
#include "WaterPump.h"
#include "HX710C.h"
#include "wifi.h"
#include "ADC.h"
#include "timer.h"
#include "led.h"
#include "RTC.h"
#include "key.h"
#include "flash.h"

/* FreeRTOS includes */
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#include "list.h"

/*LJC */
static TaskHandle_t xHandleTaskKeyToggle = NULL;
static TaskHandle_t xHandleTaskWeightTest = NULL;
static TaskHandle_t xHandleTaskJudgeTest = NULL;

static void UserTaskCreate(void);

void vTaskKeyToggle(void* pvParameters);
void vTaskAWeightTest(void* pvParameters);
void vTaskAJudgeTest(void* pvParameters);


int main(void)
{   
	SCB->VTOR = FMC_BASE | 0x1800;
	USART1_Init();       //wifi串口初始化  波特率均为9600
	USART2_Init();	     // 调试串口初始化  
	printf("154\r\n");
	LED_Init();           //led灯初始化
	AIP_GPIO_Init();        // 灯带引脚初始化
	AIP33624_Init();        // 数码管初始化
	LED_Water_Wight(10);    //显示灯的数量	
	delay_ms(200);
	        
	Waterpump_Init();       //水泵初始化
	wifi_protocol_init();   //WiFi初始化
	HX710C_Init();          //称重模块初始化
	APM_MINI_TMR14_Init();  //定时器14初始化
	APM_MINI_TMR3_Init();   //定时器3初始化
	APM_MINI_TMR6_Init();   //定时器6初始化
	
	RTC_Init();            //RTC初始化
	DMA_Init();         //DMA通道初始化
	ADC_Init();         //ADC初始化
	ADC_StartConversion();  //ADC启动转化命令
	WIFI_EN(1);         //开启WIFI
	Key_Init();        //按键初始化
	Read_B_K_Cali();       //读取水量跟K的值以及更换剩余天数
	
/*************flash********************************************/
//	float fdata = 3.15415;
//	u32 Date_F =989;
//	u32 user_data;
//	u8 filter_time = 10;
//	u8 Pure_time = 11;
//	u8 Clear_time_report =12;
//	
//	EarseFlash_1K(User_Data);//擦除数据
//	WriteFlash(User_Data,(uint8_t *)&Date_F,4);//写入指定的数据
//	WriteFlash(User_Data+4,(uint8_t *)&fdata,4);//写入指定的数据
//	WriteFlash(User_Data+8,(uint8_t *)&filter_time,4);//写入
//	WriteFlash(User_Data+12,(uint8_t *)&Pure_time,4);//写入
//	WriteFlash(User_Data+16,(uint8_t *)&Clear_time_report,4);//写入

//	
//	user_data = ReadFlashTest(User_Data);
//	printf("user_data = %d\r\n",user_data);
//	printf("user= %f\r\n", ReadFlash(User_Data+4));
//	printf("filter_time=%d\r\n",ReadFlashTest(User_Data+4+4));
//	printf("Pure_time=%d\r\n",ReadFlashTest(User_Data+12));
//	printf("Clear_time_report= %d\r\n",ReadFlashTest(User_Data+16));
/************************************************************/
	UserTaskCreate();
  vTaskStartScheduler();
 
    while(1)
    {
			
//		ADC_MultiChannelPolling();	 //ADC采集电压
			
    }
		
}


static void UserTaskCreate(void)
{
    xTaskCreate(vTaskKeyToggle,
                "TaskKeyToggle",
                256,
                NULL,
                2,
                &xHandleTaskKeyToggle);

    xTaskCreate(vTaskAWeightTest,
                "TaskWeightTest",
                128,
                NULL,
                1,
                &xHandleTaskWeightTest);
	 xTaskCreate(vTaskAJudgeTest,
                "TaskJudgeTest",
                128,
                NULL,
                0,
                &xHandleTaskJudgeTest);
}



void vTaskKeyToggle(void* pvParameters)
{
	while(1)
    {	

			Key_Tick();     //判断按键标志位
			HALL_Cheek();	  //按键操作
			wifi_uart_service();   //WiFi需要在循环中一直调用
			WIFI_LED(mcu_get_wifi_work_state());  //WiFi配网指示灯
			vTaskDelay(15);
		}
		
}

void vTaskAWeightTest(void* pvParameters)
{
	while(1)
    {	
			Calc_flash();        //校准
			wifi_uart_service();   //WiFi需要在循环中一直调用
			timer_day_report();    //更新剩余天数

			vTaskDelay(1000);
		}
}


void vTaskAJudgeTest(void* pvParameters)
{
	while(1)
    {		
			Write_K_B_clac();        //存flash
			Judge_drink_weight();    //饮水量上报
			vTaskDelay(3000);
		}
}






